/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.server.embedded;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.security.ProtectionDomain;
import java.util.Properties;

/**
 *
 * @author Walter L�tgenau
 */
public class NodesConfig {

    public static final String CONFIG_FILE = "nodes.properties";
    public static final String CONFIG_REPOSITORY = "repository";
    public static final String CONFIG_INSTALL_DIR = "installDir";
    public static final String CONFIG_PORT = "port";
    public static final String DEFAULT_REPOSITORY = "nodes";
    File repository;
    File installDir;
    int port;
    boolean installed = false;
    boolean showBrowser = false;

    public NodesConfig() {}

    public NodesConfig(File installDir, File repository, int port, boolean showBrowser) {
        this.repository = repository;
        this.installDir = installDir;
        this.port = port;
        this.showBrowser = showBrowser;
    }

    public File getInstallDir() {
        return installDir;
    }

    public void setInstallDir(File installDir) {
        this.installDir = installDir;
    }
    
    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public File getRepository() {
        return repository;
    }

    public void setRepository(File repository) {
        this.repository = repository;
    }

    public boolean isInstalled() {
        return installed;
    }

    public boolean isShowBrowser() {
        return showBrowser;
    }

    public void setShowBrowser(boolean showBrowser) {
        this.showBrowser = showBrowser;
    }

    /**
     * Return the current directory, i.e. the directory with the jar.
     * 
     * @return
     */
    protected File getCurrentDirectory() throws MalformedURLException, URISyntaxException {
        ProtectionDomain protectionDomain = Launcher.class.getProtectionDomain();
        String war = protectionDomain.getCodeSource().getLocation().toExternalForm();
        URL url = new URL(war);
        File warFile = new File(url.toURI());
        return warFile.getParentFile();
    }

    /**
     * Set the default configuration values
     */
    protected void setDefaults() throws MalformedURLException, URISyntaxException {
        setInstallDir(getCurrentDirectory());
        setRepository(new File(getCurrentDirectory().getPath() + File.separator + DEFAULT_REPOSITORY));
        setPort(8080);
    }

    /**
     * Load the configuration from the properties file. If this does not yet exist, set the defaults as well as for missing properties.
     * 
     * @throws IOException
     */
    public void load() throws IOException, MalformedURLException, URISyntaxException {
        setDefaults();

        File configFile = new File(getCurrentDirectory() + File.separator + CONFIG_FILE);
        if (configFile.exists()) {
            installed = true;
            
            Properties config = new Properties();
            config.load(new FileInputStream(configFile));

            if (config.getProperty(CONFIG_INSTALL_DIR) != null)
                setInstallDir(new File(config.getProperty(CONFIG_INSTALL_DIR)));
            if (config.getProperty(CONFIG_REPOSITORY) != null)
                setRepository(new File(config.getProperty(CONFIG_REPOSITORY)));
            if (config.getProperty(CONFIG_PORT) != null)
                try {
                    setPort(Integer.parseInt(config.getProperty(CONFIG_REPOSITORY)));
                } catch (NumberFormatException e) {

                }
        }
    }

    /**
     * Store the configuration in a properties file at the install location.
     * 
     * @throws IOException
     */
    public void store() throws IOException {
        File configFile = new File(getInstallDir().getPath() + File.separator + CONFIG_FILE);
        if (!configFile.exists())
            configFile.createNewFile();

        Properties properties = new Properties();
        properties.setProperty(CONFIG_INSTALL_DIR, getInstallDir().getPath());
        properties.setProperty(CONFIG_REPOSITORY, getRepository().getPath());
        properties.setProperty(CONFIG_PORT, "" + getPort());
        
        properties.store(new FileOutputStream(configFile), "nodes properties");
    }
}
